﻿using DevExpress.XtraEditors;
using FuelDXApplication1.Entity;
using FuelDXApplication1.MksWinform;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FuelDXApplication1.HqhWinform
{
    public partial class SupplementaryTransmission : DevExpress.XtraEditors.XtraForm
    {
        private int pageIndex = 1;
        private int pageSize = 2;
        int pageCount;
        int TotalCount;

        public SupplementaryTransmission()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 补传待上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SupplementaryTransmission_Load(object sender, EventArgs e)
        {
            await GetVehicleRType();
            await GetFuelType();
            await GetSupplementayList();
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task GetSupplementayList()
        {

            PageGetBase pagegetbase = new(pageIndex, pageSize);

            var dataTable = from a in DBHelper.Query<BasicInformation>().Result
                            join b in DBHelper.Query<FuelType>().Result on a.FuelTypeId equals b.FuelTypeId
                            join c in DBHelper.Query<VehicleTypeInFo>().Result on a.VersionId equals c.VehicleTypeId
                            select new
                            {
                                a.FuelParameter,
                                a.FeedbackCode,
                                a.ICPRecordNumber,
                                a.AutomobileManufacturers,
                                a.DateOfImport,
                                a.DateInfo,
                                a.VehicleModel,
                                a.Dealer,
                                b.FuelTypeName,
                                c.VehicleTypeName,
                            };
            TotalCount = dataTable.Count();
            pageCount = (int)Math.Ceiling(TotalCount * 1.0 / pagegetbase.pageSize);

            var pageInfo = dataTable.OrderBy(x => x.FuelParameter).Skip((pagegetbase.pageIndex - 1) * pagegetbase.pageSize).Take(pagegetbase.pageSize);
            gridControl1.DataSource = pageInfo;
        }


        /// <summary>
        /// 车辆种类
        /// </summary>
        /// <returns></returns>
        private async Task GetVehicleRType()
        {
            var VehicleRType = await DBHelper.Query<VehicleTypeInFo>();
            this.lookUpEdit1.Properties.DataSource = VehicleRType;
            this.lookUpEdit1.Properties.ValueMember = "VehicleTypeId";
            this.lookUpEdit1.Properties.DisplayMember = "VehicleTypeName";
        }


        /// <summary>
        /// 燃料类型
        /// </summary>
        /// <returns></returns>
        private async Task GetFuelType()
        {
            var FuelType = await DBHelper.Query<FuelType>();
            this.lookUpEdit2.Properties.DataSource = FuelType;
            this.lookUpEdit2.Properties.ValueMember = "FuelTypeId";
            this.lookUpEdit2.Properties.DisplayMember = "FuelTypeName";
        }

        /// <summary>
        /// 补传待上报查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {

            string ICPRecordNumber = textEdit1.Text;
            var VersionId = lookUpEdit1.EditValue;
            var SdateDateOfImport = dateEdit1.EditValue; //开始日期
            string VehicleModel = textEdit3.Text;
            var FuelTypeId = lookUpEdit2.EditValue;
            var EndateDateOfImport = dateEdit2.EditValue; //结束日期
            string datestr = comboBoxEdit4.Text;//时间类型

            var list = DBHelper.Query<BasicInformation>();
            if (list.Result != null)
            {
                var result = list.Result.AsQueryable();
                if (!string.IsNullOrEmpty(ICPRecordNumber))
                {
                    result = result.Where(x => x.ICPRecordNumber.Contains(ICPRecordNumber));
                }

                if (VersionId != null)
                {
                    result = result.Where(x => x.VersionId == Convert.ToInt32(VersionId));
                }
                if (SdateDateOfImport != null)
                {
                    result = result.Where(x => x.DateOfImport >= Convert.ToDateTime(SdateDateOfImport));
                }

                if (!string.IsNullOrEmpty(VehicleModel))
                {
                    result = result.Where(x => x.VehicleModel == VehicleModel);
                }
                if (FuelTypeId != null)
                {
                    result = result.Where(x => x.FuelTypeId == (int)FuelTypeId);
                }
                if (EndateDateOfImport != null)
                {
                    result = result.Where(x => x.DateOfImport <= Convert.ToDateTime(EndateDateOfImport));
                }
                if (!string.IsNullOrEmpty(datestr))
                {
                    result = result.Where(x => x.FuelTypeId == Convert.ToInt32(datestr));
                }

                // 更新Grid数据源
                gridControl1.DataSource = result.ToList();
            }
            else
            {
                gridControl1.DataSource = null;
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton2_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            GetSupplementayList();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            pageIndex = pageCount;
            GetSupplementayList();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
                GetSupplementayList();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (pageIndex < TotalCount)
            {
                pageIndex++;
                GetSupplementayList();
            }
        }

        /// <summary>
        /// 补传待上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton6_Click(object sender, EventArgs e)
        {
            await UpdateStatues();
        }

        /// <summary>
        /// 补传待上报
        /// </summary>
        /// <returns></returns>
        public async Task UpdateStatues()
        {
            // 获取选中的行索引
            int[] rownumber = this.gridView1.GetSelectedRows();

            // 遍历选中的行索引
            foreach (int rowIndex in rownumber)
            {
                // 获取当前行的 BasicInformation 数据项
                var item = this.gridView1.GetRow(rowIndex) as BasicInformation;

                var res = await HttpClientHelper.RequestUrl<BasicInformation, ApiResult<string>>(
                                "https://localhost:44341/",
                                "api/app/fuel-synchronization/basic-inform",
                                MethodType.Post,
                                item);

                if (res.Code == 200)
                {
                    FeedbackCode feedbackCode = new FeedbackCode(item.ICPRecordNumber, item.FeedbackCode);
                    feedbackCode.Show();
                    return;
                }
            }
        }


        public record PageGetBase(int pageIndex, int pageSize);
    }
}